package org.yogthos;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.PathModifier;
import org.anddev.andengine.entity.modifier.PathModifier.IPathModifierListener;
import org.anddev.andengine.entity.modifier.PathModifier.Path;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.Debug;
import org.anddev.andengine.util.modifier.ease.EaseSineInOut;

import android.widget.Toast;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class PathModifierExample extends BaseExample {
        // ===========================================================
        // Constants
        // ===========================================================

        private static final int CAMERA_WIDTH = 720;
        private static final int CAMERA_HEIGHT = 480;

        // ===========================================================
        // Fields
        // ===========================================================

        private Camera mCamera;

        private RepeatingSpriteBackground mGrassBackground;

        private BitmapTextureAtlas mBitmapTextureAtlas;
        private TiledTextureRegion mPlayerTextureRegion;

        // ===========================================================
        // Constructors
        // ===========================================================

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================


        public Engine onLoadEngine() {
                Toast.makeText(this, "You move my sprite right round, right round...", Toast.LENGTH_LONG).show();
                this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
                return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
        }


        public void onLoadResources() {
                this.mBitmapTextureAtlas = new BitmapTextureAtlas(128, 128, TextureOptions.DEFAULT);
                BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
                this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);
                //this.mGrassBackground = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, this.mEngine.getTextureManager(), new AssetBitmapTextureAtlasSource(this, "background_grass.png"));

                this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
        }


        public Scene onLoadScene() {
//              this.mEngine.registerUpdateHandler(new FPSLogger());

                final Scene scene = new Scene();
                //scene.setBackground(this.mGrassBackground);

                /* Create the face and add it to the scene. */
                final AnimatedSprite player = new AnimatedSprite(10, 10, 48, 64, this.mPlayerTextureRegion);

                final Path path = new Path(5).to(10, 10).to(10, CAMERA_HEIGHT - 74).to(CAMERA_WIDTH - 58, CAMERA_HEIGHT - 74).to(CAMERA_WIDTH - 58, 10).to(10, 10);
                
                /* Add the proper animation when a waypoint of the path is passed. */
                player.registerEntityModifier(new LoopEntityModifier(new PathModifier(30, path, null, new IPathModifierListener() {
                     
                        public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
                                Debug.d("onPathStarted");
                        }

                   
                        public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
                                Debug.d("onPathWaypointStarted:  " + pWaypointIndex);
                                switch(pWaypointIndex) {
                                        case 0:
                                                player.animate(new long[]{200, 200, 200}, 6, 8, true);
                                                break;
                                        case 1:
                                                player.animate(new long[]{200, 200, 200}, 3, 5, true);
                                                break;
                                        case 2:
                                                player.animate(new long[]{200, 200, 200}, 0, 2, true);
                                                break;
                                        case 3:
                                                player.animate(new long[]{200, 200, 200}, 9, 11, true);
                                                break;
                                }
                        }

                
                        public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
                                Debug.d("onPathWaypointFinished: " + pWaypointIndex);
                        }

                 
                        public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
                                Debug.d("onPathFinished");
                        }
                }, EaseSineInOut.getInstance())));
                scene.attachChild(player);

                return scene;
        }

   
        public void onLoadComplete() {

        }

        // ===========================================================
        // Methods
        // ===========================================================

        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
}